package com.situ.meetingRoom.controller;

import com.situ.meetingRoom.entity.Boardroom;
import com.situ.meetingRoom.entity.Result;
import com.situ.meetingRoom.service.BoardroomService;
import com.situ.meetingRoom.service.impl.BoardroomServiceImpl;
import com.situ.meetingRoom.util.BaseServlet;
import com.situ.meetingRoom.util.PageInfo;
import com.situ.meetingRoom.vo.BoardroomVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "BoardroomServlet", value = "/boardroom")
public class BoardroomServlet extends BaseServlet {

    private BoardroomService boardroomService = new BoardroomServiceImpl();

    public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //分页
        //获取参数
        String sPage = request.getParameter("page");
        String sLimit = request.getParameter("limit");
        //1-获取参数
        String name = request.getParameter("name");
        String address = request.getParameter("address");
        String sStatus = request.getParameter("status");

        Boardroom boardroom = new Boardroom();
        boardroom.setName(name);
        boardroom.setAddress(address);

        if (sStatus != null && sStatus.length() > 0) {
            boardroom.setStatus(Integer.parseInt(sStatus));
        }

        Integer page = null, limit = null;
        try {
            page = Integer.parseInt(sPage);
            limit = Integer.parseInt(sLimit);
        } catch (Exception e) {
        }
        //判断是否分页
        if (page != null) {
            //分页
            PageInfo pageInfo = boardroomService.getForPage(page, limit, boardroom);
            response.setContentType("application/json; charset=UTf-8");
            response.getWriter().append(Result.ok(pageInfo).toJson());
        } else {
            List<BoardroomVO> boardrooms = boardroomService.getAll(boardroom);

            //返回，以JSON格式返回
            response.setContentType("application/json; charset=UTf-8");
            response.getWriter().append(Result.ok(boardrooms).toJson());
        }
    }

    public void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1-获取参数
        String sId = request.getParameter("id");
        try {
            Integer id = Integer.parseInt(sId);

            //2-调用service
            boardroomService.remove(id);

            //3-返回数据
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append(Result.ok().toJson());
        } catch (Exception e) {
            e.printStackTrace();
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append(Result.error(e.getMessage()).toJson());
        }
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1-获取参数
        String name = request.getParameter("name");
        String capacity = request.getParameter("capacity");
        String address = request.getParameter("address");
        //2-参数校验

        //3-调用service
        Boardroom boardroom = new Boardroom();
        boardroom.setName(name);
        boardroom.setCapacity(capacity);
        boardroom.setAddress(address);

        try {
            boardroomService.add(boardroom);
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append(Result.ok().toJson());
        } catch (Exception e) {
            e.printStackTrace();
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append(Result.error(e.getMessage()).toJson());
        }
    }

    public void getById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1-获取参数
        String sId = request.getParameter("id");

        Integer id = Integer.parseInt(sId);

        //2-调用service
        Boardroom boardroom = boardroomService.getById(id);

        response.setContentType("application/json; charset=UTF-8");
        response.getWriter().append(Result.ok(boardroom).toJson());


    }

    public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1-获取参数
        String sId = request.getParameter("id");
        String name = request.getParameter("name");
        String capacity = request.getParameter("capacity");
        String address = request.getParameter("address");
        Integer id = Integer.parseInt(sId);
        //2-参数校验

        //3-调用service
        Boardroom boardroom = new Boardroom();
        boardroom.setId(id);
        boardroom.setName(name);
        boardroom.setCapacity(capacity);
        boardroom.setAddress(address);

        try {
            boardroomService.edit(boardroom);
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append(Result.ok().toJson());
        } catch (Exception e) {
            e.printStackTrace();
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append(Result.error(e.getMessage()).toJson());
        }
    }


}
